<?php

namespace App\Http\Controllers;

use App\Services\ConstantsService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;

/**
 * 常量控制器
 * 提供常量获取、验证、缓存管理等接口
 */
class ConstantsController extends Controller
{
    public function __construct(
        private ConstantsService $constantsService
    ) {}

    /**
     * 获取所有常量
     */
    public function index(): JsonResponse
    {
        try {
            $constants = $this->constantsService->getAllConstants();
            
            return response()->json([
                'success' => true,
                'data' => $constants,
                'message' => '常量获取成功'
            ]);
        } catch (\Exception $e) {
            Log::error('获取常量失败', ['error' => $e->getMessage()]);
            
            return response()->json([
                'success' => false,
                'message' => '常量获取失败',
                'error' => config('app.debug') ? $e->getMessage() : null
            ], 500);
        }
    }

    /**
     * 获取指定模块的常量
     */
    public function show(string $module): JsonResponse
    {
        try {
            $constants = $this->constantsService->getModuleConstants($module);
            
            if (empty($constants)) {
                return response()->json([
                    'success' => false,
                    'message' => '模块不存在或没有常量'
                ], 404);
            }
            
            return response()->json([
                'success' => true,
                'data' => $constants,
                'message' => '常量获取成功'
            ]);
        } catch (\Exception $e) {
            Log::error('获取模块常量失败', [
                'module' => $module,
                'error' => $e->getMessage()
            ]);
            
            return response()->json([
                'success' => false,
                'message' => '常量获取失败',
                'error' => config('app.debug') ? $e->getMessage() : null
            ], 500);
        }
    }

    /**
     * 验证常量值
     */
    public function validate(Request $request): JsonResponse
    {
        $request->validate([
            'module' => 'required|string',
            'value' => 'required'
        ]);

        try {
            $module = $request->input('module');
            $value = $request->input('value');
            
            $isValid = $this->constantsService->isValidConstant($module, $value);
            
            return response()->json([
                'success' => true,
                'data' => [
                    'module' => $module,
                    'value' => $value,
                    'is_valid' => $isValid
                ],
                'message' => $isValid ? '常量值有效' : '常量值无效'
            ]);
        } catch (\Exception $e) {
            Log::error('验证常量值失败', [
                'module' => $request->input('module'),
                'value' => $request->input('value'),
                'error' => $e->getMessage()
            ]);
            
            return response()->json([
                'success' => false,
                'message' => '常量值验证失败',
                'error' => config('app.debug') ? $e->getMessage() : null
            ], 500);
        }
    }

    /**
     * 刷新常量缓存
     */
    public function refresh(): JsonResponse
    {
        try {
            $this->constantsService->refreshCache();
            
            return response()->json([
                'success' => true,
                'message' => '常量缓存刷新成功'
            ]);
        } catch (\Exception $e) {
            Log::error('刷新常量缓存失败', ['error' => $e->getMessage()]);
            
            return response()->json([
                'success' => false,
                'message' => '常量缓存刷新失败',
                'error' => config('app.debug') ? $e->getMessage() : null
            ], 500);
        }
    }

    /**
     * 获取常量统计信息
     */
    public function statistics(): JsonResponse
    {
        try {
            $stats = $this->constantsService->getStatistics();
            
            return response()->json([
                'success' => true,
                'data' => $stats,
                'message' => '统计信息获取成功'
            ]);
        } catch (\Exception $e) {
            Log::error('获取常量统计信息失败', ['error' => $e->getMessage()]);
            
            return response()->json([
                'success' => false,
                'message' => '统计信息获取失败',
                'error' => config('app.debug') ? $e->getMessage() : null
            ], 500);
        }
    }
}
